<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="author" content="李愧愧">
    <title></title>
    <style>
        li{
            list-style: none;
            
            
        }
    </style>
</head>

<body>
    <ul>
    </ul>
    <script>
        var ul = document.querySelector("ul")
        // 创建xhr对象
        var xhr = new XMLHttpRequest();
        // 设置请求方式和请求地址url
        xhr.open("get", "http://192.168.14.249:5000/news");
        // 发送请求
        xhr.send();
        console.log(xhr);
        //监听请求状态获得请求结果, 绑定readystatechange事件，当请求状态改变时触发
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                console.log(xhr.responseText)
                var jsonResult = JSON.parse(xhr.responseText);
                var stories = jsonResult.stories;
                console.log(stories)
                stories.forEach(function (item,index){
                    var li = document.createElement("li");
                    ul.appendChild(li);
                    console.log(item)
                    // 解析news新闻
                    var img = document.createElement("img")
                    //  这就是json解析： 准确的从json结构中找到想要数据并展示出来
                    img.src = item.images[0]
                    img.style.verticalAlign = "middle"
                    li.appendChild(img);
                    var a = document.createElement("a")
                    a.innerHTML = item.title;
                    a.setAttribute=("href","#");
                    a.href=item.url;
                    // a.style=""
                    li.appendChild(a);
                    a.style="margin-left: 10px; text-decoration: none;color:gray;font-weight: bold;"
                });
            }
        }
    </script>
</body>

</html>